import GCJ_CLASS_TYPE=require('./../GCJ_CLASS_TYPE');
import DEFINE=require('./../DEFINE');
import Serializer=require('../../kernel/base/Serializer')
import Parser=require('../../kernel/base/Parser')
import ObjBase=require('../../kernel/obj/ObjBase')
import SJGRelationV3=require('./SJGRelationV3');
import SJGNode=require('./../tg/SJGNode');
//
export = SJGNodeV3;
/**
 * 题纲的节点
 * @author 
 *
 */
class SJGNodeV3 extends SJGNode{
    // 附加属性
    // 注意：对于根节点的直接子节点中的叶节点可以有多个relation，对于编组节点只能有一个relation
    public relations: Array<SJGRelationV3>=[];// 节点和版面对象的对应关系,［在分页映射时设置］
    
    public constructor(g?) {
        super(g);
        
        this.ct=GCJ_CLASS_TYPE.CLASS_TYPE_SJGNodeV3;
	}
    // 转成json字符串
    public toJson(): string {
        // 去掉父json字符串的头尾
        var superJson=super.toJson();
        superJson=superJson.substr(1,superJson.length-2);

        var ret='{';
        ret+=superJson;
        
        // relations
        ret+=',';
        ret+='"relations":';
        ret+='[';
        for(var i=0;i<this.relations.length;i++) {
            if(i>0) {
                ret+=',';
            }
            ret+=this.relations[i].toJson();
        }
        ret+=']';
       
        ret+='}';
        return ret;
    }
    // 创建新实例
    public newInstance() {
        return new SJGNodeV3();
    }
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        super.fromJson(jsonObj,versionId);

        if(jsonObj.hasOwnProperty("relations")){
            for(var i=0;i<jsonObj.relations.length;i++) {
                var o=jsonObj.relations[i];
                var r: SJGRelationV3=new SJGRelationV3();
                r.page_index=o.page_index;
                r.obj_id=o.obj_id;
                r.page_content_index=0;// page_content_index 初始化成与page_index相同
                this.relations.push(r);
            }
        }
    }
    // copy
    public clone():SJGNodeV3{
        var json=this.toJson();
        var ret: SJGNodeV3=new SJGNodeV3();
        ret.fromJson(JSON.parse(json),SJGNode.VERSION_3);
        ret.parent=this.parent; // fromJson不靠背parent，需要显示拷贝
        return ret;
    }

    public checkPage(): Boolean {
        for(var i = 0; i < this.relations.length; i++) {
            var r: SJGRelationV3=this.relations[i];
            if(r.page_index==null) return false;
        }
        
        for(var i=0;i<this.children.length;i++) {
            var child=this.children[i];
            if(!child.checkPage()) return false;
        }
        return true;
    }
    
    // 将当前节点的page_index设置到子上
    public set_page_index_to_child() {
        var pi=this.relations[0].page_index;
        var pci=this.relations[0].page_content_index;
        for(var i=0;i<this.children.length;i++) {
            var child: SJGNodeV3=<SJGNodeV3>(this.children[i]);
            if(child) {
                child.relations[0].page_index=pi;
                child.relations[0].page_content_index=pci;
                child.set_page_index_to_child();
            }
        }
    }
    // 是否为题纲节点对应的文字
    public is_tg_node_obj(id: number): Boolean {
        for(var i=0;i<this.relations.length;i++) {
            var r: SJGRelationV3=this.relations[i];
            if(r.obj_id!=null&&r.obj_id==id) {
                return true;
            }
        }
        
        
        for(var i=0;i<this.children.length;i++) {
            var child=this.children[i];
            if(child&&child.is_tg_node_obj(id)) {
                return true;
            }
        }
        return false;
    }
    
    // 将page_index大于指定值的节点加上偏移
    public updatePageIndex(page_index, offset) {
        for(var i=0;i<this.relations.length;i++) {
            var r: SJGRelationV3=this.relations[i];
            if(r.page_index>page_index) {
                r.page_index+=offset;
            }
        }
        
        for(var i=0;i<this.children.length;i++) {
            var child=this.children[i];
            child.updatePageIndex(page_index,offset);
        }
    }
    
    
    
    
    
    // 根据page_index找到relation
    public getRelation(page_index:number){
        for(var i=0;i<this.relations.length;i++) {
            var r =this.relations[i];
            if(r.page_index == page_index){
                return r;
            }
        }
        return null;
    }
    
    // 根据obj_id取得 node
    public getNodeByObjId(obj_id: number): Object {
        for(var k=0;k<this.relations.length;k++){
            var r: SJGRelationV3=this.relations[k];
            if(r.obj_id==obj_id) return this;    
        }
        
        
        for(var i=0;i<this.children.length;i++) {
            var child=<SJGNodeV3>this.children[i];
            var n=child.getNodeByObjId(obj_id);
            if(n) return n;
        }
        return null;
    }
    
    
}//SJG_MARK_END-------------------------